package com.ulic.customerManApplication.documentmodule.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.documentmodule.bo.Document;
import com.ulic.customerManApplication.documentmodule.service.DocumentService;
import com.ulic.customerManApplication.documentmodule.vo.DocumentVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.util.config.ConfigInfo;
import com.ulic.customerManApplication.util.excel.DownloadFile;
import com.ulic.customerManApplication.util.excel.UploadFile;
@Component("documentAction")
@Scope("prototype")
public class DocumentAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DocumentService documentService;
	private String fileName;
	private File documentFile;
	private Document document;
	
	public Document getDocument() {
		return document;
	}
	public void setDocument(Document document) {
		this.document = document;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public File getDocumentFile() {
		return documentFile;
	}
	public void setDocumentFile(File documentFile) {
		this.documentFile = documentFile;
	}
	public DocumentService getDocumentService() {
		return documentService;
	}
	@Resource
	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		PagerModel pm = documentService.getDocuments(curPage);
		httpServletRequest.setAttribute("pm",pm);
		return SUCCESS;
	}
	
	public String searchDocuments()throws Exception{
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		String startDate  = httpServletRequest.getParameter("startDate");
		String endDate  = httpServletRequest.getParameter("endDate");
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		DocumentVO documentVO = new DocumentVO();
		documentVO.setStartDate(startDate);
		documentVO.setEndDate(endDate);
		documentVO.setDocument(document);
		documentVO.setUser(user);
		documentVO.setRoleName(roleName);
		PagerModel pm = documentService.getDocuments(documentVO,curPage);
		httpServletRequest.setAttribute("pm",pm);
		httpServletRequest.setAttribute("startDate", startDate);
		httpServletRequest.setAttribute("endDate", endDate);
		httpServletRequest.setAttribute("success",httpServletRequest.getParameter("success"));
		return SUCCESS;
	}
	
	public String impDocument()throws Exception{
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		long date = new Date().getTime();
		String doc = null;
		if(documentFile != null && documentFile.length() > 0){
			if(documentFile.length()<=10485760){
				
				if(fileName != null && fileName.length() !=0){
					try {
						doc = fileName.substring(fileName.lastIndexOf("\\")+1);
						fileName = UploadFile.upload(documentFile, fileName,date);//先将文件上传到服务器
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				Date now = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String impDate = sdf.format(now);
				document.setUploadDate(impDate);
				document.setUploadPerson(user);
				document.setDocument(doc+":"+fileName);
				documentService.addDocument(document);
				PagerModel pm = documentService.getDocuments(curPage);
				httpServletRequest.setAttribute("pm",pm);
				return SUCCESS;
			}else{
				httpServletRequest.setAttribute("error", 2);
				return ERROR;
			}
		}else{
			httpServletRequest.setAttribute("error", 1);
			return ERROR;
		}
	}
	
	public String expDocument()throws Exception{
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
		Document doc = documentService.getDocument(document.getId());
		String realPath = ConfigInfo.getPropertiesValue("uploadfile.path");
		DownloadFile.download(realPath,doc.getDocument(),httpServletRequest,httpServletResponse);//从服务器上下载文件
		return null;
	}
	public String deleteDocument()throws Exception{
		document = documentService.getDocument(document.getId());
		documentService.removeDocument(document);
		return SUCCESS;
	}
}
